package com.easylinkin.linkappapi.grid.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.core.utils.HttpFileUtils;
import com.easylinkin.linkappapi.common.model.BaseController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.grid.models.dto.GridReportWeekSaveDTO;
import com.easylinkin.linkappapi.grid.models.query.GridReportWeekQuery;
import com.easylinkin.linkappapi.grid.models.vo.GridReportWeekInfoVO;
import com.easylinkin.linkappapi.grid.models.vo.GridReportWeekListVO;
import com.easylinkin.linkappapi.grid.service.IGridReportWeekService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.Arrays;


/**
 * 网格周报
 *
 * @author kuyujie
 * @since 2024-05-17
 */
@Api(tags = "网格周报")
@Setter(onMethod_ = @Resource)
@RestController
@RequestMapping("/grid/gridReportWeek")
public class GridReportWeekController extends BaseController {
    private IGridReportWeekService gridReportWeekService;

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<GridReportWeekQuery> requestModel) {
        this.setOperator(requestModel.getCustomQueryParams());
        IPage<GridReportWeekListVO> data = gridReportWeekService.pageList(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(data).build();
    }

    /**
     * 导出
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "导出", notes = "导出")
    @PostMapping(value = "/export")
    public ResponseEntity<byte[]> export(@RequestBody GridReportWeekQuery query) {
        this.setOperator(query);
        byte[] bytes = gridReportWeekService.export(query);
        return HttpFileUtils.exportFile(bytes, String.format("网格周报导出(%s).zip", DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss")));
    }

    /**
     * 保存
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping(value = "/save")
    public RestMessage save(@RequestBody GridReportWeekSaveDTO dto) {
        this.setOperator(dto);
        gridReportWeekService.saveData(dto);
        return RestBuilders.successBuilder().message("保存成功").build();
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/delete")
    public RestMessage delete(@RequestParam("id") Integer id) {
        gridReportWeekService.removeById(id);
        return RestBuilders.successBuilder().message("删除成功").build();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public RestMessage deleteBatch(@RequestParam("ids") String ids) {
        this.gridReportWeekService.removeByIds(Arrays.asList(ids.split(",")));
        return RestBuilders.successBuilder().message("批量删除成功").build();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public RestMessage queryById(@RequestParam("id") Long id) {
        GridReportWeekInfoVO data = gridReportWeekService.getInfo(id);
        return RestBuilders.successBuilder().data(data).build();
    }
}
